package com.fcscs.cservice.client.main.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.common.MainPaginationSearchModel;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SearchExecutor;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.inroomdining.InRoomDiningModel;
import com.fcscs.cservice.client.common.model.inroomdining.InRoomDiningModelData;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class InRoomDiningGrid extends
		PagingSearchResultsPanel<InRoomDiningModelData, MainPaginationSearchModel> implements
		FormSubmitListener<InRoomDiningModel>, ModelSelectListener<InRoomDiningModelData> {
	private InRoomDiningDetailDialog inRoomDiningDetailDialog;

	public InRoomDiningGrid(String title, SearchExecutor<InRoomDiningModelData, MainPaginationSearchModel> exe) {
		super(title, exe, (Object[]) null);
		setSize(958, 400);
	}

	@Override
	public void onModelClick(InRoomDiningModelData model) {

	}

	@Override
	public void onModelDoubleClick(InRoomDiningModelData model) {

	}

	@Override
	public void onModelEnterPress(InRoomDiningModelData model) {

	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<InRoomDiningModel> form, SubmitType submitType) {

	}

	@Override
	protected List<ColumnConfig> getColumnConfig(Object... params) {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig cc = CoreUtil.createNonSortableColumnConf(ShareGWTConstants.ROOM,
				ShareGWTConstants.SHARE_GWT_CONSTANTS.room(), 120);
		columns.add(cc);

		cc = CoreUtil.createNonSortableColumnConf(CServiceConstants.GUESTNAME,
				CServiceConstants.CSERVICE_CONSTANTS.guestName(),150);
		columns.add(cc);

		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.CATEGORY,
				CServiceConstants.CSERVICE_CONSTANTS.category(), 150));
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.DELIVERYDATE,
				CServiceConstants.CSERVICE_CONSTANTS.deliveryDate(), 150));
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.DELIVERYTIME,
				CServiceConstants.CSERVICE_CONSTANTS.deliveryTime(), 150));
		columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.STATUS,
				CoreConstants.CORE_CONSTANTS.status(), 100));
		cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
				CoreConstants.CORE_CONSTANTS.action(), 80);

		cc.setRenderer(new GridCellRenderer<InRoomDiningModelData>() {

			@Override
			public Object render(InRoomDiningModelData model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<InRoomDiningModelData> store,
					Grid<InRoomDiningModelData> grid) {
				HorizontalPanel hr = new HorizontalPanel();
				hr.setHorizontalAlign(HorizontalAlignment.LEFT);

				HighLightIconButton ib = createEditBtn(model);
				ib.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
				ib.setId("PayTypeGrid_edit_button");
				hr.add(ib);
				return hr;
			}
		});
		columns.add(cc);
		return columns;
	}
	
	private HighLightIconButton createEditBtn(final InRoomDiningModelData model) {
		HighLightIconButton iconBtn = new HighLightIconButton(ShareGWTConstants.CSS_EDIT_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
			@Override
			public void componentSelected(IconButtonEvent ce) {
				showEditDeviceDialog(model);
			}
		});
		iconBtn.setId("DeviceGridPanel_create_editbtn_id");
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.edit());
		return iconBtn;
	}
	
	private void showEditDeviceDialog(InRoomDiningModelData model) {
		inRoomDiningDetailDialog = InRoomDiningDetailDialog.getInstance();
		inRoomDiningDetailDialog.getFormPanel().updateForm(model.getEntity());
		inRoomDiningDetailDialog.show();
	}
}
